<?php

namespace app\service;

use app\controller\SystemUser;
use app\model\OrderDeal;
use app\model\OrderDealChargeback;
use app\model\OrderEnterpriseCertificateBusiness;
use app\model\OrderEnterpriseCertificateRegister;
use app\model\OrderEnterpriseCompany;
use app\model\OrderEnterpriseExchange;
use app\model\OrderLog;
use app\model\OrderOtherCertificateBusiness;
use app\model\OrderPersonCertificateRegister;
use app\model\OrderTransactionLog;
use app\model\OrderTransactionRecord;
use app\model\UserMessage;
use app\model\UserOrganization;
use app\model\UserPosition;
use app\stateData;
use think\facade\Cache;
use think\facade\Db;

class FinanceServer
{
    private $eq = 'eq';
    private $between = 'between';
    private $like = 'like';
    private $in = 'in';
    /**
     * 财务
     */
    //获取列表
    /**
     * 原业绩总表 逻辑正确 加载缓慢
     */
//    public function getEachDetails($data, $userIdData)
//    {
//        $business = new OrderEnterpriseCertificateBusiness();
//        $enter = new OrderEnterpriseCertificateRegister();
//        $otherCerBusiness = new OrderOtherCertificateBusiness();
//        $company = new OrderEnterpriseCompany();
//        $exchange = new OrderEnterpriseExchange();
//        $messages = new UserMessage();
//        $personShModel = new OrderPersonCertificateRegister();
//
//        $personOrder = new OrderPersonCertificateRegister();
//        $systemModel = new System();
//        $transaction = new OrderTransactionRecord(); //收付款表
//        $deal = new OrderDeal();
//        $chargebackModel = new OrderDealChargeback();
//
//        //查询退单
//        $chargebackData = $chargebackModel->getCharDataModel();
//
//        $result = [];
//        $fdata = [];
//
//        //查询证书办理订单
//        $others = $otherCerBusiness->getRandDataModel($userIdData);
//
//        if (!empty($others)) {
//            foreach ($others as $other) {
//
//                //查询所有有附属业务的人才信息
//                $personData = $personShModel->getAncillaryIdNotEmptyModel ();
//
//                //查询该办理绑定人才订单是否已出售 已出售删除
//                $bool = true;
//                foreach ($personData as $iValue) {
//                    if ($iValue['ancillary_businesses_id'] && $iValue['person_id_card']) {
//                        //判断该人才身份证是否已经出售
//                        if ($company->getIdCardOrderInfoModel ($iValue['person_id_card'])) {
//                            $oidArr = explode (',', rtrim ($iValue['ancillary_businesses_id'], ','));
//                            if (in_array ($other['id'], $oidArr)) {
//                                $bool = false;
//                            }
//                        }
//                    }
//                }
//                if (!$bool) {
//                    continue;
//                }
//                $fdata = $other;
//                //处理拼接返回表格参数
//                //出证价
//                $fdata['sale_price'] = $other['cost_price'];
//                $fdata['contract_price'] = $other['sale_price'];
//                //业绩
////                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
//                //最新收款时间
//                $fdata['get_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 1) ?: "";
//                //已收款合计
//                $skall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 1);
//                $fdata['get_price_all'] = $skall['all_price'] ?: '';
//                //最新付款时间
//                $fdata['out_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 2) ?: '';
//                //已付款合计
//                $fkall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 2);
//                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
//                $fdata['leixing'] = "证书办理";
//                $fdata['out_uid'] = $fdata['get_uid'];
//                $fdata['out_org'] = $fdata['get_org'];
//                //用于财务状态搜索status
//                $fdata['cw_status'] = $fdata['status'];
//
//                //查询有没有退单
//                $tData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
//                if ($tData){
//                    $fdata['tuidan'] = true;
//                }else{
//                    $fdata['tuidan'] = false;
//                }
//                if($tData && $fdata['status']['text'] === '已退单'){
//                    $fdata['td_performance'] = $tData['performance'];
//                    if ($tData['type'] == 1){
//                        $fdata['td_date'] = $tData['create_date'];
//                    }else{
//                        $fdata['td_date'] = '';
//                    }
//
//                    $fdata['reason'] = $tData['reason'];
//                }else{
//                    $fdata['td_performance'] = '';
//                    $fdata['td_date'] = '';
//                    $fdata['reason'] = '';
//                }
//
//                //最新一条催款记录
//                $ckData = $messages->getPressFindEmpty ($fdata['id'], $fdata['certificate_code']);
//
//                if (!empty($ckData)) {
//                    $fdata['ckjl'] = 1; //有
//                } else {
//                    $fdata['ckjl'] = 2;//没有
//                }
//                $result[] = array_merge (['order_status' => ''], $fdata);
//            }
//            //添加退单完成
////            if (!empty($chargebackData)) {
////                foreach ($chargebackData as $chargebackDatum) {
////                    $tData = $otherCerBusiness->getCerDataModel ($chargebackDatum['certificate_code']);
////                    //判断个人还是管理 增加响应退单内容
//////                        $idArr = array_column ($datas,'id');
////                    if ($tData) {
////                        //权限判断
////                        if (!in_array ($tData['get_uid']['id'], $userIdData)) {
////                            continue;
////                        }
//////                            if(in_array ($tData['id'],$idArr)){
//////                        if ($chargebackDatum['performance'] > 0){
//////                            $tData['performance'] = '-'.$chargebackDatum['performance'];
//////                        }else{
////                        $tData['td_performance'] = $chargebackDatum['performance'];
//////                        }
////
////                        $tData['create_date'] = $chargebackDatum['create_date'];
////                        $tData['tuidan'] = true;
////                        $tData['reason'] = $chargebackDatum['reason'];
////                        $tData['cw_status'] = [
////                            'id' => 999,
////                            'text' => '退单完成'
////                        ];
////                        $tData['status'] = [
////                            'id' => 999,
////                            'text' => '退单完成'
////                        ];
////                        //催款记录
////                        $tData['ckjl'] = 2;//没有
////                        $result[] = $tData;
//////                            }
////                    }
////                }
////            }
//        }
//
//        //查询企业证书
//        $businesss = $business->getRandDataModel($userIdData);
//        if (!empty($businesss)) {
//
//            foreach ($businesss as $busineItem) {
//                $fdata = $busineItem;
//
//                //处理拼接返回表格参数
//                //出证价
//                $fdata['contract_price'] = $fdata['sale_price'];
//                $fdata['sale_price'] = $fdata['cost_price'];
//                //业绩
////                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
//                //最新收款时间
//                $fdata['get_price_date'] = $transaction->getFindDataModel ( $fdata['certificate_code'],$fdata['id'], 1) ?: '';
//                //已收款合计
//                $skall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 1);
//
//                $fdata['get_price_all'] = $skall['all_price'] ?: '';
//                //最新付款时间
//                $fdata['out_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 2) ?: '';
//                if ($fdata['out_price_date'] === '0000-00-00 00:00:00'){
//                    $fdata['out_price_date'] = '';
//                }
//
//                //已付款合计
//                $fkall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 2);
//                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
//                $fdata['leixing'] = "企业证书";
//                //最新一条催款记录
//                $ckData = $messages->getPressFindEmpty ($fdata['id'], $fdata['certificate_code']);
//
//                //用于财务状态搜索status
//                $fdata['cw_status'] = $fdata['status'];
//                //查询有没有退单
//                $tData = $chargebackModel->getCidCerDataModel ($fdata['id'],$fdata['certificate_code']);
//                if ($tData){
//                    $fdata['tuidan'] = true;
//                }else{
//                    $fdata['tuidan'] = false;
//                }
//                if($tData && $fdata['status']['text'] === '已退单'){
//                    $fdata['td_performance'] = $tData['performance'];
//                    if ($tData['type'] == 1){
//                        $fdata['td_date'] = $tData['create_date'];
//                    }else{
//                        $fdata['td_date'] = '';
//                    }
//                    $fdata['reason'] = $tData['reason'];
//                }else{
//                    $fdata['td_performance'] = '';
//                    $fdata['td_date'] = '';
//                    $fdata['reason'] = '';
//                }
//
//                if (!empty($ckData)) {
//                    $fdata['ckjl'] = 1; //有
//                } else {
//                    $fdata['ckjl'] = 2;//没有
//                }
//
//                $result[] = array_merge (['order_status' => ''], $fdata);
//            }
//            //添加退单完成
////            if (!empty($chargebackData)) {
////                foreach ($chargebackData as $chargebackDatum) {
////                    $tData = $business->getCerDataModel ($chargebackDatum['certificate_code']);
////                    //判断个人还是管理 增加响应退单内容
//////                        $idArr = array_column ($datas,'id');
////                    if ($tData) {
////                        //权限判断
////                        if (!in_array ($tData['get_uid']['id'], $userIdData) && !in_array ($tData['out_uid']['id'], $userIdData)) {
////                            continue;
////                        }
//////                            if(in_array ($tData['id'],$idArr)){
//////                        if ($chargebackDatum['performance'] > 0){
//////                            $tData['performance'] = '-'.$chargebackDatum['performance'];
//////                        }else{
////                        $tData['td_performance'] = $chargebackDatum['performance'];
//////                        }
////
////                        $tData['create_date'] = $chargebackDatum['create_date'];
////                        $tData['tuidan'] = true;
////                        $tData['reason'] = $chargebackDatum['reason'];
////                        $tData['cw_status'] = [
////                            'id' => 999,
////                            'text' => '退单完成'
////                        ];
////                        $tData['status'] = [
////                            'id' => 999,
////                            'text' => '退单完成'
////                        ];
////                        //催款记录
////                        $tData['ckjl'] = 2;//没有
////                        $result[] = $tData;
//////                            }
////                    }
////                }
////            }
//        }
//
//        //证书注册与倒证
//        foreach ($data as $key => $datum) {
//            if (!isset($datum['order_status'])) {
//                $datum['order_status'] = $deal->getOrderIdStatusModel($datum['id'], $datum['certificate_code']);
//            }
//
//            //查询是否是企业证书注册
//            $companys = $company->getCerIdFindModel($datum['order_company_id'],$datum['certificate_code']);
//
//            if (!empty($companys)){
//
//                $fdata = $companys;
//
//                //处理拼接返回表格参数
//                $fdata['contract_price'] = $companys['price_unit'];
//                $fdata['sale_price'] = $companys['contract_price'];
//                //业绩
////                    $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
//                //最新收款时间
//                $fdata['get_price_date'] = $transaction->getFindDataModel( $fdata['certificate_code'],$fdata['id'], 1) ?: '';
//                //已收款合计
//                $skall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 1);
//                $fdata['get_price_all'] = $skall['all_price'] ?: '';
//                //最新付款时间
//                $fdata['out_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 2) ?: '';
//                //已付款合计
//                $fkall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 2);
//                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
//                $fdata['leixing'] = "公司证书";
//                $fdata['out_uid'] = $fdata['sale_uid'];
//                $fdata['out_org'] = $fdata['sale_org'];
//                //用于财务状态搜索status
//                $fdata['cw_status'] = $fdata['status'];
//                //证书处理
//                if ($fdata['certificate_leixing']) {
//                    $fdata['certificate_leixing'] = [
//                        'id' => $fdata['certificate_id'][0],
//                        'text' => $fdata['certificate_leixing']
//                    ];
//                } else {
//                    $fdata['certificate_leixing'] = [
//                        'id' => '',
//                        'text' => $fdata['certificate_leixing']
//                    ];
//                }
//                if ($fdata['certificate_leibie']) {
//                    $fdata['certificate_leibie'] = [
//                        'id' => $fdata['certificate_id'][1],
//                        'text' => $fdata['certificate_leibie']
//                    ];
//                } else {
//                    $fdata['certificate_leibie'] = [
//                        'id' => '',
//                        'text' => $fdata['certificate_leibie']
//                    ];
//                }
//                if ($fdata['certificate_dengji']) {
//                    $fdata['certificate_dengji'] = [
//                        'id' => $fdata['certificate_id'][2],
//                        'text' => $fdata['certificate_dengji']
//                    ];
//                } else {
//                    $fdata['certificate_dengji'] = [
//                        'id' => '',
//                        'text' => $fdata['certificate_dengji']
//                    ];
//                }
//                if ($fdata['certificate_zhuanye']) {
//                    $fdata['certificate_zhuanye'] = [
//                        'id' => $fdata['certificate_id'][3],
//                        'text' => $fdata['certificate_zhuanye']
//                    ];
//                } else {
//                    $fdata['certificate_zhuanye'] = [
//                        'id' => '',
//                        'text' => $fdata['certificate_zhuanye']
//                    ];
//                }
//
//                //最新一条催款记录
//                $ckData = $messages->getPressFindEmpty($fdata['id'], $fdata['certificate_code']);
//                if (!empty($ckData)) {
//                    $fdata['ckjl'] = 1; //有
//                } else {
//                    $fdata['ckjl'] = 2;//没有
//                }
//
//                //查询绑定的人才附属业务
//                if (!empty($fdata['person_id_card'])) {
//
//                    $ancillaryStr = rtrim($personOrder->getIdCardAncillary($fdata['person_id_card']), ',');
//                    //附属id不为空 查询对应证书办理内容
//                    if ($ancillaryStr) {
//
//                        $ancillaryArr = explode(',', $ancillaryStr);
//                        foreach ($ancillaryArr as $ancillaryId) {
//                            $childs = $this->getOtherBusiness($ancillaryId);
//                            if ($childs){
//                                $fdata['childs'][] = $childs;
//                            }
//                        }
//                    }
//                }
//
//                //权限判断
//                if (!in_array($fdata['get_uid']['id'], $userIdData) && !in_array($fdata['out_uid']['id'], $userIdData)) {
//                    continue;
//                }
//
//
//                //退单完成信息
//                $tData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
//                if ($tData){
//                    $fdata['tuidan'] = true;
//                }else{
//                    $fdata['tuidan'] = false;
//                }
//                if ($tData && $fdata['status']['text'] === '已退单'){
//                    $fdata['td_performance'] = $tData['performance'];
//                    if ($tData['type'] == 1){
//                        $fdata['td_date'] = $tData['create_date'];
//                    }else{
//                        $fdata['td_date'] = '';
//                    }
//
//                    $fdata['reason'] = $tData['reason'];
//                }else{
//                    $fdata['td_performance'] = '';
//                    $fdata['td_date'] = '';
//
//                    $fdata['reason'] = '';
//                }
//
//                $result[] = array_merge(['order_status' => $datum['order_status']], $fdata);
//                continue;
//
//            }
//
//            //查询是否是倒证成交
//            $exchanges = $exchange->getFinanceFieldFindModel($datum['certificate_code']);
//            if (!empty($exchanges)) {
//                $fdata = $exchanges;
//                //处理拼接返回表格参数
//                //收证价
//                $fdata['sale_price'] = $fdata['get_price'] ?: 0;
//                //出证价
//                $fdata['contract_price'] = $fdata['out_price'] ?: 0;
//                //业绩
////                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
//                //最新收款时间
//                $fdata['get_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 1) ?: '';
//                //已收款合计
//                $skall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 1);
//                $fdata['get_price_all'] = $skall['all_price'] ?: '';
//                //最新付款时间
//                $fdata['out_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 2) ?: '';
//                //已付款合计
//                $fkall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 2);
//                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
//                $fdata['leixing'] = "倒证";
//                $fdata['business_id'] = "倒证";
//                //用于财务状态搜索status
//                $fdata['cw_status'] = $fdata['status'];
//                //最新一条催款记录
//                $ckData = $messages->getPressFindEmpty($fdata['id'], $fdata['certificate_code']);
//                if (!empty($ckData)) {
//                    $fdata['ckjl'] = 1; //有
//                } else {
//                    $fdata['ckjl'] = 2;//没有
//                }
//
//                //权限判断
//                if (!in_array($fdata['get_uid']['id'], $userIdData) && !in_array($fdata['out_uid']['id'], $userIdData)) {
//                    continue;
//                }
//
//                //退单完成信息
//                $cData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
//                if ($cData){
//                    $fdata['tuidan'] = true;
//                }else{
//                    $fdata['tuidan'] = false;
//                }
//
//                if ($cData && $fdata['status']['text'] === '已退单'){
//                    $fdata['td_performance'] = $cData['performance'];
//                    if ($cData['type'] == 1){
//                        $fdata['td_date'] = $cData['create_date'];
//                    }else{
//                        $fdata['td_date'] = '';
//                    }
//                    $fdata['reason'] = $cData['reason'];
//
//                }else{
//                    $fdata['td_performance'] = '';
//                    $fdata['td_date'] = '';
//                    $fdata['reason'] = '';
//                }
//                $result[] = array_merge(['order_status' => $datum['order_status']], $fdata);
//            }
//        }
//
//        return array_values($result);
//    }

    public function getEachDetails($id,$cer, $data, $userIdData)
    {
        $business = new OrderEnterpriseCertificateBusiness();
        $otherCerBusiness = new OrderOtherCertificateBusiness();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $messages = new UserMessage();
        $personShModel = new OrderPersonCertificateRegister();

        $personOrder = new OrderPersonCertificateRegister();
        $transaction = new OrderTransactionRecord(); //收付款表
        $deal = new OrderDeal();
        $chargebackModel = new OrderDealChargeback();

        if (is_one_dimensional_array ($data)){
            $data = array($data);
        }

        $result = [];

        //查询证书办理订单
        $othersData = $otherCerBusiness->getWhereIdDataSelectModel($id,$cer);
        if (!empty($othersData)) {

            foreach ($othersData as $other) {

                //查询所有有附属业务的人才信息
                $personData = $personShModel->getAncillaryIdNotEmptyModel ();

                //查询该办理绑定人才订单是否已出售 已出售删除
                $bool = true;
                foreach ($personData as $iValue) {
                    if ($iValue['ancillary_businesses_id'] && $iValue['person_id_card']) {
                        //判断该人才身份证是否已经出售
                        if ($company->getIdCardOrderInfoModel ($iValue['person_id_card'])) {
                            $oidArr = explode (',', rtrim ($iValue['ancillary_businesses_id'], ','));
                            if (in_array ($other['id'], $oidArr)) {
                                $bool = false;
                            }
                        }
                    }
                }
                if (!$bool) {
                    continue;
                }
                $fdata = $other;
                //处理拼接返回表格参数
                //出证价
                $fdata['sale_price'] = $other['cost_price'];
                $fdata['contract_price'] = $other['sale_price'];
                //业绩
//                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
                //最新收款时间
                $fdata['get_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 1) ?: "";
                //已收款合计
                $skall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 1);
                $fdata['get_price_all'] = $skall['all_price'] ?: '';
                //最新付款时间
                $fdata['out_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 2) ?: '';
                //已付款合计
                $fkall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 2);
                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
                $fdata['leixing'] = "证书办理";
                $fdata['out_uid'] = $fdata['get_uid'];
                $fdata['out_org'] = $fdata['get_org'];
                //用于财务状态搜索status
                $fdata['cw_status'] = $fdata['status'];

                //查询有没有退单
                $tData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
                if ($tData){
                    $fdata['tuidan'] = true;
                }else{
                    $fdata['tuidan'] = false;
                }
                if($tData && $fdata['status']['text'] === '已退单'){
                    $fdata['td_performance'] = $tData['performance'];
                    if ($tData['type'] == 1){
                        $fdata['td_date'] = $tData['create_date'];
                    }else{
                        $fdata['td_date'] = '';
                    }

                    $fdata['reason'] = $tData['reason'];
                }else{
                    $fdata['td_performance'] = '';
                    $fdata['td_date'] = '';
                    $fdata['reason'] = '';
                }

                //最新一条催款记录
                $ckData = $messages->getPressFindEmpty ($fdata['id'], $fdata['certificate_code']);

                if (!empty($ckData)) {
                    $fdata['ckjl'] = 1; //有
                } else {
                    $fdata['ckjl'] = 2;//没有
                }
                $result = array_merge (['order_status' => ''], $fdata);
            }
            //有数据直接返回 订单ID cer 只可能对应一个订单
            return $result;
        }

        //查询企业证书
        $businesssData = $business->getWhereIdDataSelectModel($id,$cer);
        if (!empty($businesssData)) {
            foreach ($businesssData as $busineItem) {
                $fdata = $busineItem;

                //处理拼接返回表格参数
                //出证价
                $fdata['contract_price'] = $fdata['sale_price'];
                $fdata['sale_price'] = $fdata['cost_price'];
                //业绩
//                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
                //最新收款时间
                $fdata['get_price_date'] = $transaction->getFindDataModel ( $fdata['certificate_code'],$fdata['id'], 1) ?: '';
                //已收款合计
                $skall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 1);

                $fdata['get_price_all'] = $skall['all_price'] ?: '';
                //最新付款时间
                $fdata['out_price_date'] = $transaction->getFindDataModel ($fdata['certificate_code'], $fdata['id'], 2) ?: '';
                if ($fdata['out_price_date'] === '0000-00-00 00:00:00'){
                    $fdata['out_price_date'] = '';
                }

                //已付款合计
                $fkall = $transaction->getCerTypeAllPriceModel ($fdata['certificate_code'], $fdata['id'], 2);
                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
                $fdata['leixing'] = "企业证书";
                //最新一条催款记录
                $ckData = $messages->getPressFindEmpty ($fdata['id'], $fdata['certificate_code']);

                //用于财务状态搜索status
                $fdata['cw_status'] = $fdata['status'];
                //查询有没有退单
                $tData = $chargebackModel->getCidCerDataModel ($fdata['id'],$fdata['certificate_code']);
                if ($tData){
                    $fdata['tuidan'] = true;
                }else{
                    $fdata['tuidan'] = false;
                }
                if($tData && $fdata['status']['text'] === '已退单'){
                    $fdata['td_performance'] = $tData['performance'];
                    if ($tData['type'] == 1){
                        $fdata['td_date'] = $tData['create_date'];
                    }else{
                        $fdata['td_date'] = '';
                    }
                    $fdata['reason'] = $tData['reason'];
                }else{
                    $fdata['td_performance'] = '';
                    $fdata['td_date'] = '';
                    $fdata['reason'] = '';
                }

                if (!empty($ckData)) {
                    $fdata['ckjl'] = 1; //有
                } else {
                    $fdata['ckjl'] = 2;//没有
                }

                $result = array_merge (['order_status' => ''], $fdata);
            }
            //有数据直接返回 订单ID cer 只可能对应一个订单
            return $result;
        }

        //证书注册与倒证
        foreach ($data as $datum) {

            if (!isset($datum['order_status'])) {
                $datum['order_status'] = $deal->getOrderCompanyIdFindModel($datum['id'], $datum['certificate_code'])['status'] ?? '';
            }

            //查询是否是企业证书注册
            $companys = $company->getCerIdFindModel($datum['id'],$datum['certificate_code']);

            if (!empty($companys)){

                $fdata = $companys;

                //处理拼接返回表格参数
                $fdata['contract_price'] = $companys['price_unit'];
                $fdata['sale_price'] = $companys['contract_price'];
                //业绩
//                    $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
                //最新收款时间
                $fdata['get_price_date'] = $transaction->getFindDataModel( $fdata['certificate_code'],$fdata['id'], 1) ?: '';
                //已收款合计
                $skall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 1);
                $fdata['get_price_all'] = $skall['all_price'] ?: '';
                //最新付款时间
                $fdata['out_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 2) ?: '';
                //已付款合计
                $fkall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 2);
                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
                $fdata['leixing'] = "公司证书";
                $fdata['out_uid'] = $fdata['sale_uid'];
                $fdata['out_org'] = $fdata['sale_org'];
                //用于财务状态搜索status
                $fdata['cw_status'] = $fdata['status'];
                //证书处理
                if ($fdata['certificate_leixing']) {
                    $fdata['certificate_leixing'] = [
                        'id' => $fdata['certificate_id'][0],
                        'text' => $fdata['certificate_leixing']
                    ];
                } else {
                    $fdata['certificate_leixing'] = [
                        'id' => '',
                        'text' => $fdata['certificate_leixing']
                    ];
                }
                if ($fdata['certificate_leibie']) {
                    $fdata['certificate_leibie'] = [
                        'id' => $fdata['certificate_id'][1],
                        'text' => $fdata['certificate_leibie']
                    ];
                } else {
                    $fdata['certificate_leibie'] = [
                        'id' => '',
                        'text' => $fdata['certificate_leibie']
                    ];
                }
                if ($fdata['certificate_dengji']) {
                    $fdata['certificate_dengji'] = [
                        'id' => $fdata['certificate_id'][2],
                        'text' => $fdata['certificate_dengji']
                    ];
                } else {
                    $fdata['certificate_dengji'] = [
                        'id' => '',
                        'text' => $fdata['certificate_dengji']
                    ];
                }
                if ($fdata['certificate_zhuanye']) {
                    $fdata['certificate_zhuanye'] = [
                        'id' => $fdata['certificate_id'][3],
                        'text' => $fdata['certificate_zhuanye']
                    ];
                } else {
                    $fdata['certificate_zhuanye'] = [
                        'id' => '',
                        'text' => $fdata['certificate_zhuanye']
                    ];
                }

                //最新一条催款记录
                $ckData = $messages->getPressFindEmpty($fdata['id'], $fdata['certificate_code']);
                if (!empty($ckData)) {
                    $fdata['ckjl'] = 1; //有
                } else {
                    $fdata['ckjl'] = 2;//没有
                }

                //查询绑定的人才附属业务
                if (!empty($fdata['person_id_card'])) {

                    $ancillaryStr = rtrim($personOrder->getIdCardAncillary($fdata['person_id_card']), ',');
                    //附属id不为空 查询对应证书办理内容
                    if ($ancillaryStr) {

                        $ancillaryArr = explode(',', $ancillaryStr);
                        foreach ($ancillaryArr as $ancillaryId) {
                            $childs = $this->getOtherBusiness($ancillaryId);
                            if ($childs){
                                $fdata['childs'][] = $childs;
                            }
                        }
                    }
                }

                //权限判断
                if (!in_array($fdata['get_uid']['id'], $userIdData) && !in_array($fdata['out_uid']['id'], $userIdData)) {
                    continue;
                }


                //退单完成信息
                $tData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
                if ($tData){
                    $fdata['tuidan'] = true;
                }else{
                    $fdata['tuidan'] = false;
                }
                if ($tData && $fdata['status']['text'] === '已退单'){
                    $fdata['td_performance'] = $tData['performance'];
                    if ($tData['type'] == 1){
                        $fdata['td_date'] = $tData['create_date'];
                    }else{
                        $fdata['td_date'] = '';
                    }

                    $fdata['reason'] = $tData['reason'];
                }else{
                    $fdata['td_performance'] = '';
                    $fdata['td_date'] = '';

                    $fdata['reason'] = '';
                }

                $result = array_merge(['order_status' => $datum['order_status']], $fdata);
                break;
            }

            //查询是否是倒证成交
            $exchanges = $exchange->getFinanceFieldFindModel($datum['certificate_code']);
            if (!empty($exchanges)) {
                $fdata = $exchanges;
                //处理拼接返回表格参数
                //收证价
                $fdata['sale_price'] = $fdata['get_price'] ?: 0;
                //出证价
                $fdata['contract_price'] = $fdata['out_price'] ?: 0;
                //业绩
//                $fdata['performance'] = $fdata['sale_price']-$fdata['contract_price'];
                //最新收款时间
                $fdata['get_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 1) ?: '';
                //已收款合计
                $skall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 1);
                $fdata['get_price_all'] = $skall['all_price'] ?: '';
                //最新付款时间
                $fdata['out_price_date'] = $transaction->getFindDataModel($fdata['certificate_code'], $fdata['id'], 2) ?: '';
                //已付款合计
                $fkall = $transaction->getCerTypeAllPriceModel($fdata['certificate_code'], $fdata['id'], 2);
                $fdata['out_price_all'] = $fkall['all_price'] ?: '';
                $fdata['leixing'] = "倒证";
                $fdata['business_id'] = "倒证";
                //用于财务状态搜索status
                $fdata['cw_status'] = $fdata['status'];
                //最新一条催款记录
                $ckData = $messages->getPressFindEmpty($fdata['id'], $fdata['certificate_code']);
                if (!empty($ckData)) {
                    $fdata['ckjl'] = 1; //有
                } else {
                    $fdata['ckjl'] = 2;//没有
                }

                //权限判断
                if (!in_array($fdata['get_uid']['id'], $userIdData) && !in_array($fdata['out_uid']['id'], $userIdData)) {
                    continue;
                }

                //退单完成信息
                $cData = $chargebackModel->getCidCerDataModel($fdata['id'],$fdata['certificate_code']);
                if ($cData){
                    $fdata['tuidan'] = true;
                }else{
                    $fdata['tuidan'] = false;
                }

                if ($cData && $fdata['status']['text'] === '已退单'){
                    $fdata['td_performance'] = $cData['performance'];
                    if ($cData['type'] == 1){
                        $fdata['td_date'] = $cData['create_date'];
                    }else{
                        $fdata['td_date'] = '';
                    }
                    $fdata['reason'] = $cData['reason'];

                }else{
                    $fdata['td_performance'] = '';
                    $fdata['td_date'] = '';
                    $fdata['reason'] = '';
                }
                $result = array_merge(['order_status' => $datum['order_status']], $fdata);
            }

        }

        return $result;
    }


    //补全证书详情信息
    public function supInfo($data)
    {

        if (empty($data)) {
            return $data;
        }
        $datas = [
            'create_date',
            'certificate_code',
            'business_id',
            'order_status',
            'sale_org',
            'sale_uid',
            'reg_unit',
            'price_unit',
            'certificate_leixing',
            'certificate_leibie',
            'certificate_dengji',
            'certificate_zhuanye',
            'certificate_b',
            'social_security',
            'cycle',
            'project',
            'people_num',
            'get_org',
            'get_uid',
            'get_price',
            'person_name',
            'person_id_card',
            'reg_from_illustrate',
            'employ_unit_title',
            'employ_unit_name',
            'employ_unit_mobile',
            'performance',
            'electronic_certificate_file_illustrate',
            'electronic_certificate_date',
            'b_electronic_certificate_file',
            'b_electronic_certificate_date',
            'b_reg_electronic_certificate_date'
        ];

        foreach ($datas as $iValue) {

            if (!array_key_exists($iValue, $data)) {
                $data[$iValue] = '';
            }
        }
        return $data;
    }


    //获取对应type id 内容
    public function getBusinessDetails($id, $type, $cer)
    {

        $business = new OrderEnterpriseCertificateBusiness();
        $enter = new OrderEnterpriseCertificateRegister();
        $other = new OrderOtherCertificateBusiness();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();


        $result = [];
        if ($type == 1) {
            $result = $this->supInfo($company->getCertificateCodeFindDataModel($id, $cer));
            $result['chudanfangshi'] = '证书注册';
//            $result['performance'] = number_format($result['price_unit']/$result['people_num']-$result['contract_price'],2);
//            $result['performance'] = $result['price_unit']/$result['people_num']-$result['contract_price'];
        }

        if ($type == 2) {
            $result = $this->supInfo($exchange->getWhereCerDataModel($cer));
            $result['chudanfangshi'] = '倒证';
//            $result['performance'] = number_format($result['out_price']-$result['get_price'],2);
        }

        if ($type == 3) {
            $result = $this->supInfo($other->getCerDataModel($cer));
            $result['chudanfangshi'] = '证书办理';
//            $result['performance'] = number_format($result['sale_price']-$result['cost_price'],2);
        }

        if ($type == 4) {
            $result = $this->supInfo($business->getCerDataModel($cer));
            $result['chudanfangshi'] = '企业证书';
//            $result['performance'] = number_format($result['sale_price']-$result['cost_price'],2);
        }

        return $result;
    }

    //通过订单id 寻找成交id->收付款内容
    public function getCollectionPaymentList($id, $transaction_code, $type): ?array
    {
        $dealModel = new OrderDeal();
        return (new OrderTransactionRecord())->getPassOrderTransactionStatus($id, $transaction_code, $type);
    }


    //获取导出需要补充材料
    public function ecxelSuppInfo($data)
    {
        $business = new OrderEnterpriseCertificateBusiness();
        $enter = new OrderEnterpriseCertificateRegister();
        $otherCerBusiness = new OrderOtherCertificateBusiness();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $transaModel = new OrderTransactionRecord();

        $personOrder = new OrderPersonCertificateRegister();

        $result = '';
        foreach ($data as $key => &$datum) {

            //查询是否是企业证书
            if (!empty($business->getFinanceFieldFindModel($datum['certificate_code']))) {
                $result = $business->getExcelInfoModel($datum['certificate_code']);
                if (!empty($result)) {
                    $datum['person_name'] = '';
                    $datum['sale_price'] = $result['sale_price'] ?? '';
                    $datum['get_price'] = '';
                    $datum['out_unit'] = $result['out_unit'] ?? '';
                    $datum['employ_unit'] = $result['employ_unit'] ?? '';
                    $datum['reg_unit'] = $result['reg_unit'] ?? '';
                    $datum['yifu'] = $transaModel->getOrderStatusYsModel($datum['certificate_code'], 1, 2)['all_price'] ?: 0;
                } else {
                    $datum['person_name'] = '';
                    $datum['sale_price'] = '';
                    $datum['get_price'] = '';
                    $datum['out_unit'] = '';
                    $datum['employ_unit'] = '';
                    $datum['reg_unit'] = '';
                    $datum['yifu'] = '';
                }
            }
//
//            //查询是否是证书办理
            if (!empty($otherCerBusiness->getFinanceFieldFindModel($datum['certificate_code']))) {
                $result = $otherCerBusiness->getExcelInfoModel($datum['certificate_code']);
                if (!empty($result)) {
                    $datum['person_name'] = $result['person_name'] ?? '';
                    $datum['sale_price'] = $result['sale_price'] ?? '';
                    $datum['get_price'] = $result['get_price'] ?? '';
                    $datum['out_unit'] = $result['out_unit'] ?? '';
                    $datum['employ_unit'] = $result['employ_unit'] ?? '';
                    $datum['reg_unit'] = $result['reg_unit'] ?? '';
                } else {
                    $datum['person_name'] = '';
                    $datum['sale_price'] = '';
                    $datum['get_price'] = '';
                    $datum['out_unit'] = '';
                    $datum['employ_unit'] = '';
                    $datum['reg_unit'] = '';
                }
            }

            //查询是否是企业证书注册
            if (!empty($company->getCertificateCodeFindModel($datum['certificate_code']))) {

                //获取应收价 注册单位
                $regData = $enter->getExcelInfoModel($datum['certificate_code']);
                //获取聘用单位
                $companyData = $company->getExcelInfoModel($datum['certificate_code'], $datum['order_company_id']);
                if (!empty($companyData)) {
                    $datum['employ_unit'] = $companyData['employ_unit'];
                    $datum['person_name'] = $companyData['person_name'];
                    $datum['get_price'] = $companyData['get_price'];
                }

                if (!empty($regData)) {
                    $datum['sale_price'] = $regData['sale_price'];
                    $datum['reg_unit'] = $regData['reg_unit'];
                    $datum['out_unit'] = '';
                }
            }

            //查询是否是倒证成交
            if (!empty($exchange->getFinanceFieldFindModel($datum['certificate_code']))) {
                $result = $exchange->getExcelInfoModel($datum['order_company_id']);
                if (!empty($result)) {
                    $datum['person_name'] = $result['person_name'] ?? '';
                    $datum['sale_price'] = $result['sale_price'] ?? '';
                    $datum['get_price'] = $result['get_price'] ?? '';
                    $datum['out_unit'] = $result['out_unit'] ?? '';
                    $datum['employ_unit'] = $result['employ_unit'] ?? '';
                    $datum['reg_unit'] = $result['reg_unit'] ?? '';
                } else {
                    $datum['person_name'] = '';
                    $datum['sale_price'] = '';
                    $datum['get_price'] = '';
                    $datum['out_unit'] = '';
                    $datum['employ_unit'] = '';
                    $datum['reg_unit'] = '';
                }
            }
        }
        return $data;
    }


    //获取是哪类证书
    public function getCertificateType($certificateCode): int
    {
        $business = new OrderEnterpriseCertificateBusiness();
        $otherCerBusiness = new OrderOtherCertificateBusiness();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $personModel = new OrderPersonCertificateRegister();

        $type = 0;
        //企业证书业务
        if (!empty($business->getFinanceFieldFindModel($certificateCode))) {
            $type = 4;
        }

        //查询是否是证书办理
        if (!empty($otherCerBusiness->getFinanceFieldFindModel($certificateCode))) {
            $type = 3;

        }

        //查询是否是企业证书注册
        if (!empty($company->getCertificateCodeFindModel($certificateCode))) {

            $type = 1;
        }

        //查询是否是倒证成交
        if (!empty($exchange->getFinanceFieldFindModel($certificateCode))) {
            $type = 2;
        }

        if (!empty($personModel->getCerFindModel($certificateCode))) {
            $type = 99;
        }
        return $type;
    }

    //补全收款单应收款 已收款 应付款
    public function ysPrice($data)
    {
        $business = new OrderEnterpriseCertificateBusiness();
        $otherCerBusiness = new OrderOtherCertificateBusiness();
        $company = new OrderEnterpriseCompany();
        $exchange = new OrderEnterpriseExchange();
        $transaction = new OrderTransactionRecord();
        $systemService = new System();
        $orgModel = new UserOrganization();
        $transactionLog = new OrderTransactionLog();
        $personModel = new OrderPersonCertificateRegister();
        $orderDealModel = new OrderDeal();

        if (empty($data)) {
            return "";
        }

        //循环寻找订单集合每个订单应收款与已收款
        foreach ($data as $i => $iValue) {

            //获取该订单是哪种类型订单
            $type = $this->getCertificateType($iValue['certificate_code']);
            //初始化内容
            $data[$i]['yingshou'] = 0;
            //查询应付款
            $data[$i]['yingfu'] = 0;

            //查询已收款
            $data[$i]['yishou'] = 0;
            //查询已付
            $data[$i]['yifu'] = 0;

            //查询id对应的收付款日志
            $data[$i]['transaction_log'] = $transactionLog->getIdDataModel($iValue['id']);

            if (isset($iValue['order_company_id'])) {
                $id = $iValue['order_company_id'];
            } else {
                $id = $iValue['id'];
            }

            //证书注册
            if ($type == 1) {

                //查询应收款
                $comData = $company->getYingshouModel($id, $iValue['certificate_code']);

                //企业证书注册应收款为单位价除以人数
                if ($comData['people_num'] == 0) $comData['people_num'] = 1;

                $data[$i]['yingshou'] = $comData['price_unit'] ?:'';
                //查询应付款
                if (isset($comData['certificate_id'][3])){
                    $cid = $comData['certificate_id'][3];
                }else{
                    $cid = 0;
                }
                //查询订单属于续签还是已售出
                if ($orderDealModel->getCerComIdFindModel ($id, $iValue['certificate_code']) === '续签'){
                    $data[$i]['yingfu'] = $comData['contract_price'];
                }else{
                    $data[$i]['yingfu'] = $personModel->getPersonIdCardCerOrderModel($comData['person_id_card'],$cid)['person_price']?:0;
                }

                //查询已收款
                $data[$i]['yishou'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'],1,1)['all_price'], 2) ?: 0;
                //查询已付
                $data[$i]['yifu'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'], 1, 2)['all_price'], 2) ?: 0;

            }
            if ($type == 2) {

                //倒证应收款
                $data[$i]['yingshou'] = round($exchange->getCerGetPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询应付款
                $data[$i]['yingfu'] = round($exchange->getCerOutPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询已收款
                $data[$i]['yishou'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'],1,1)['all_price'], 2) ?: 0;
                //查询已付
                $data[$i]['yifu'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'], 1, 2)['all_price'], 2) ?: 0;
            }
            if ($type == 3) {
                //证书办理应收款
                $data[$i]['yingshou'] = round($otherCerBusiness->getCerGetPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询应付款
                $data[$i]['yingfu'] = round($otherCerBusiness->getCerOutPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询已收款
                $data[$i]['yishou'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'],1,1)['all_price'], 2) ?: 0;
                //查询已付
                $data[$i]['yifu'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'], 1, 2)['all_price'], 2) ?: 0;
            }
            if ($type == 4) {
                //企业证书业务应收款
                $data[$i]['yingshou'] = round($business->getCerGetPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询应付款
                $data[$i]['yingfu'] = round($business->getCerOutPriceModel($iValue['certificate_code']), 2) ?: 0;
                //查询已收款
                $data[$i]['yishou'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'],1,1)['all_price'], 2) ?: 0;
                //查询已付
                $data[$i]['yifu'] = round($transaction->getOrderStatusIdDateYsModel($iValue['id'],$iValue['create_date'], $id, $iValue['certificate_code'], 1, 2)['all_price'], 2) ?: 0;
            }

            if (isset($iValue['transaction_code'])) {
                //订单编号总计收款
                $data[$i]['sum_get_price'] = round($transaction->getTransactionCodeSumModel($iValue['transaction_code'])['sum_get_price'], 2);
            }

            //拼接部门完整路径返回给前段
            $data[$i]['org_complete_route'] = $systemService->getOrgStrName($iValue['transaction_org']['id']);
        }
        return $data;
    }

    /**
     * 对已经查询的结果做搜索处理
     * @param $param //接收参数
     * @param $data //数据集
     */
    public function resultSearchHandle($param, $data)
    {
        /**
         * 收付款
         */
        //ID
        if (!empty($param['id'])) {
            $data = $this->search_users($data, 'id', $this->eq, $param['id']);
        }
        //付款编号
        if (!empty($param['transaction_code'])) {
            $data = $this->search_users($data, 'transaction_code', $this->like, $param['transaction_code']);
        }
        //订单编号
        if (!empty($param['certificate_code'])) {
            $data = $this->search_users($data, 'certificate_code', $this->like, $param['certificate_code']);
        }
        //业务类型
        if (!empty($param['business_id'])) {
            $data = $this->search_users($data, 'business_id', $this->eq, $param['business_id']);
        }
        //订单类型
        if (!empty($param['order_status'])) {
            if (array_key_exists((int)$param['order_status'], stateData::$_deal_status)) {
                $param['order_status'] = stateData::$_deal_status[(int)$param['order_status']];
            } else {
                $param['order_status'] = '';
            }
            $data = $this->search_users($data, 'order_status', $this->eq, $param['order_status']);
        }
        //证书类型
        if (!empty($param['certificate_leixing'])) {
            $data = $this->search_users($data, 'certificate_leixing', $this->eq, $param['certificate_leixing']);
        }

        //证书类别
        if (!empty($param['certificate_leibie'])) {
            $data = $this->search_users($data, 'certificate_leibie', $this->eq, $param['certificate_leibie']);
        }
        //证书等级
        if (!empty($param['certificate_dengji'])) {
            $data = $this->search_users($data, 'certificate_dengji', $this->eq, $param['certificate_dengji']);
        }
        //证书专业
        if (!empty($param['certificate_zhuanye'])) {
            $data = $this->search_users($data, 'certificate_zhuanye', $this->eq, $param['certificate_zhuanye']);
        }
        //人才姓名
        if (!empty($param['person_name'])) {
            $data = $this->search_users($data, 'person_name', $this->like, $param['person_name']);
        }
        //销售日期开始
        if (!empty($param['start_sale_date']) && !empty($param['end_sale_date'])) {
            $param['start_sale_date'] .= " 00:00:00";
            $param['end_sale_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'sale_date', $this->between, $param['start_sale_date'], $param['end_sale_date'], true);
        }
        if (!empty($param['sale_start_date']) && !empty($param['sale_end_date'])) {
            $param['sale_start_date'] .= " 00:00:00";
            $param['sale_end_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'sale_date', $this->between, $param['sale_start_date'], $param['sale_end_date'], true);
        }
        //出证单位
        if (!empty($param['out_unit'])) {
            $data = $this->search_users($data, 'out_unit', $this->like, $param['out_unit']);
        }
        //聘用单位
        if (!empty($param['employ_unit_title'])) {
            $data = $this->search_users($data, 'employ_unit_title', $this->like, $param['employ_unit_title']);
        }
        //注册单位
        if (!empty($param['reg_unit'])) {
            $data = $this->search_users($data, 'reg_unit', $this->like, $param['reg_unit']);
        }
        //应付金额
        if (!empty($param['meet_price'])) {
            $data = $this->search_users($data, 'meet_price', $this->between, $param['meet_price']);
        }
        //收付款应付金额
        if (
            isset($param['start_yingfu']) && ($param['start_yingfu'] || $param['start_yingfu'] === 0 || $param['start_yingfu'] === '0')
            && isset($param['end_yingfu']) && ($param['end_yingfu'] || $param['end_yingfu'] === 0 || $param['end_yingfu'] === '0')
        ) {
            $data = $this->search_users($data, 'yingfu', $this->between, $param['start_yingfu'], $param['end_yingfu']);
        }
        //收付款应收金额
        if (!empty($param['end_yingshou'])) {
            if (empty($param['start_yingshou'])) {
                $param['start_yingshou'] = 0;
            }
            $data = $this->search_users($data, 'receivable', $this->between, $param['start_yingshou'], $param['end_yingshou']);
        }
        //本次收付款金额
        if (!empty($param['end_price'])) {
            if (empty($param['start_price'])) {
                $param['start_price'] = 0;
            }
            $data = $this->search_users($data, 'price', $this->between, $param['start_price'], $param['end_price']);
        }
        //收付款类型
        if (!empty($param['type'])) {
            $data = $this->search_users($data, 'type', $this->eq, $param['type']);
        }
        //结算方式
        if (!empty($param['settlement_method'])) {
            $data = $this->search_users($data, 'settlement_method', $this->eq, $param['settlement_method']);
        }
        //提交人
        if (!empty($param['uid'])) {
            $data = $this->search_users($data, 'uid', $this->like, $param['uid']);
        }
        //收付款时间
        if (!empty($param['transaction_date'])) {
            $data = $this->search_users($data, 'transaction_date', $this->between, $param['transaction_date'],true);
        }
        //收付款事由
        if (!empty($param['notes'])) {
            $data = $this->search_users($data, 'notes', $this->like, $param['notes']);
        }

        //收证部门
        if (!empty($param['get_org'])) {
            $data = $this->search_users($data, 'get_org', $this->in, $param['get_org']);
        }
        //收证人员
        if (!empty($param['get_uid'])) {
            $data = $this->search_users($data, 'get_uid', $this->like, $param['get_uid']);
        }
        //出证部门
        if (!empty($param['out_org'])) {
            $data = $this->search_users($data, 'out_org', $this->in, $param['out_org']);
        }
        //状态
        if (isset($param['status'])) {
            if (!empty($param['status']) || $param['status'] === 0 || $param['status'] === '0') {
                $data = $this->search_users($data, 'status', $this->eq, $param['status']);
            }
        }
        //已收款区间
        if (!empty($param['end_yishou'])) {
            if (empty($param['start_yishou'])) {
                $param['start_yishou'] = 0;
            }
            $data = $this->search_users($data, 'yishou', $this->between, $param['start_yishou'], $param['end_yishou']);
        }
        //已付款区间
        if (!empty($param['end_yifu'])) {
            if (empty($param['start_yifu'])) {
                $param['start_yifu'] = 0;
            }
            $data = $this->search_users($data, 'yifu', $this->between, $param['start_yifu'], $param['end_yifu']);
        }
        //到账时间区间
        if (!empty($param['start_transaction_date']) && !empty($param['end_transaction_date'])) {
            $param['start_transaction_date'] .= " 00:00:00";
            $param['end_transaction_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'transaction_date', $this->between, $param['start_transaction_date'], $param['end_transaction_date'], true);
        }
        //提交时间
        if (!empty($param['start_submit_date']) && !empty($param['end_submit_date'])) {
            $param['start_submit_date'] .= " 00:00:00";
            $param['end_submit_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'submit_date', $this->between, $param['start_submit_date'], $param['end_submit_date'], true);
        }

        //收付款金额区间
        if (!empty($param['start_price']) && !empty($param['end_price'])) {
            $data = $this->search_users($data, 'price', $this->between, $param['start_price'], $param['end_price']);
        }
        //提交人
        if (!empty($param['transaction_uid'])) {
            $data = $this->search_users($data, 'transaction_uid', $this->like, $param['transaction_uid']);
        }
        //创建时间
        if (!empty($param['start_create_date']) && !empty($param['end_create_date'])) {
            $param['start_create_date'] .= " 00:00:00";
            $param['end_create_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'create_date', $this->between, $param['start_create_date'], $param['end_create_date'], true);
        }
        //销售部门
        if (!empty($param['sale_org'])) {
            $data = $this->search_users($data, 'sale_org', $this->in, $param['sale_org']);
        }
        //销售人员
        if (!empty($param['sale_uid'])) {
            $data = $this->search_users($data, 'sale_uid', $this->like, $param['sale_uid']);
        }
        //单位价
        if (isset($param['start_price_unit']) && !empty($param['end_price_unit'])) {
            $data = $this->search_users($data, 'price_unit', $this->between, $param['start_price_unit'], $param['end_price_unit']);
        }
        //b证
        if (!empty($param['certificate_b'])) {
            $data = $this->search_users($data, 'certificate_b', $this->eq, $param['certificate_b']);
        }
        //社保
        if (!empty($param['social_security'])) {
            $data = $this->search_users($data, 'social_security', $this->eq, $param['social_security']);
        }
        //周期
        if (!empty($param['cycle'])) {
            $data = $this->search_users($data, 'cycle', $this->eq, $param['cycle']);
        }
        //项目
        if (!empty($param['project'])) {
            $data = $this->search_users($data, 'project', $this->eq, $param['project']);
        }
        //收证价
        if (!empty($param['end_contract_price'])) {
            if (empty($param['start_contract_price'])) {
                $param['start_contract_price'] = 0;
            }
            $data = $this->search_users($data, 'contract_price', $this->between, $param['start_contract_price'], $param['end_contract_price']);
        }
        //人才身份证号码
        if (!empty($param['person_id_card'])) {
            $data = $this->search_users($data, 'person_id_card', $this->like, $param['person_id_card']);
        }
        //聘用单位联系人
        if (!empty($param['employ_unit_name'])) {
            $data = $this->search_users($data, 'employ_unit_name', $this->like, $param['employ_unit_name']);
        }
        //聘用单位联系方式
        if (!empty($param['employ_unit_mobile'])) {
            $data = $this->search_users($data, 'employ_unit_mobile', $this->like, $param['employ_unit_mobile']);
        }
        //证书有效期开始
        if (!empty($param['start_electronic_certificate_date']) || !empty($param['end_electronic_certificate_date'])) {
            $data = $this->search_users($data, 'electronic_certificate_date', $this->between, $param['start_electronic_certificate_date'], $param['end_electronic_certificate_date'], true);
        }
        //b证有效期开始
        if (!empty($param['start_b_electronic_certificate_date']) || !empty($param['end_b_electronic_certificate_date'])) {
            $data = $this->search_users($data, 'b_electronic_certificate_date', $this->between, $param['start_b_electronic_certificate_date'], $param['end_b_electronic_certificate_date'], true);
        }
        //注册到期时间
        if (!empty($param['start_b_reg_electronic_certificate_date']) || !empty($param['end_b_reg_electronic_certificate_date'])) {
            $data = $this->search_users($data, 'b_reg_electronic_certificate_date', $this->between, $param['start_b_reg_electronic_certificate_date'], $param['end_b_reg_electronic_certificate_date'], true);
        }
        //业绩
        if (!empty($param['end_performance'])) {
            if (empty($param['start_performance'])) {
                $param['start_performance'] = 0;
            }
            $data = $this->search_users($data, 'performance', $this->between, $param['start_performance'], $param['end_performance']);
        }
        //销售价
        if (!empty($param['end_sale_price'])) {
            if (empty($param['start_sale_price'])) {
                $param['start_sale_price'] = 0;
            }
            $data = $this->search_users($data, 'sale_price', $this->between, $param['start_sale_price'], $param['end_sale_price']);
        }

        /**
         * 业绩总表
         */
        //收款时间
        if (!empty($param['start_get_price_date']) || !empty($param['end_get_price_date'])) {
            $data = $this->search_users($data, 'get_price_date', $this->between, $param['start_get_price_date'], $param['end_get_price_date'], true);
        }
        //已收款
        if (!empty($param['start_get_price_all']) || !empty($param['end_get_price_all'])) {
            $data = $this->search_users($data, 'get_price_all', $this->between, $param['start_get_price_all'], $param['end_get_price_all']);
        }
        //付款时间
        if (!empty($param['start_out_price_date']) || !empty($param['end_out_price_date'])) {
            $data = $this->search_users($data, 'out_price_date', $this->between, $param['start_out_price_date'], $param['end_out_price_date'], true);
        }
        //已付款
        if (!empty($param['start_out_price_all']) || !empty($param['end_out_price_all'])) {
            $data = $this->search_users($data, 'out_price_all', $this->between, $param['start_out_price_all'], $param['end_out_price_all']);
        }
        //收付款状态
        if (!empty($param['sfk_status'])) {
            $data = $this->search_users($data, 'sfk_status', $this->eq, $param['sfk_status']);
        }
        //出证人员
        if (!empty($param['out_uid'])) {
            $data = $this->search_users($data, 'out_uid', $this->like, $param['out_uid']);
        }
        //退单时间
        if (!empty($param['start_td_date']) || !empty($param['end_td_date'])) {
            $param['start_td_date'] .= " 00:00:00";
            $param['end_td_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'td_date', $this->between, $param['start_td_date'],$param['end_td_date'],true);
        }

        //收付款完成时间
        if (!empty($param['start_sfk_wc_date']) && !empty($param['end_sfk_wc_date'])) {
            $param['start_sfk_wc_date'] .= " 00:00:00";
            $param['end_sfk_wc_date'] .= " 23:59:59";
            $data = $this->search_users($data, 'sfk_wc_date', $this->between, $param['start_sfk_wc_date'], $param['end_sfk_wc_date'], true);
        }
        //订单管理-证书注册-公司需求-状态
        if (!empty($param['gz_status'])) {
//            if(array_key_exists((int)$param['gz_status'],stateData::$_gs_status)){
//                $param['gz_status'] = stateData::$_gs_status[(int)$param['gz_status']];
//            }else{
//                $param['gz_status'] = '';
//            }
            $data = $this->search_users($data, 'gz_status', $this->eq, $param['gz_status']);
        }

        if (!empty($param['cw_status'])) {
            $data = $this->search_users($data, 'cw_status', $this->eq, $param['cw_status']);
        }

        //倒证状态筛选
        if (!empty($param['c_status'])) {
            $data = $this->search_users($data, 'status', $this->eq, $param['c_status']);
        }
        //倒证信息生成筛选
        if (!empty($param['info_generate_status'])) {
            $data = $this->search_users($data, 'info_generate_status', $this->eq, $param['info_generate_status']);
        }

        //收付款完成时间
        if (!empty($param['start_examine_date']) && !empty($param['end_examine_date'])) {
            $data = $this->search_users($data, 'examine_date', $this->between, $param['start_examine_date'], $param['end_examine_date'], true);
        }

        return $data;
    }


    /**
     * 数组内查询
     * @param $data
     * @param $keyword
     * @param $condition
     * @param $field
     * @param string $field2
     * @param bool $date
     * @return array
     */
    public function search_users($data, $keyword, $condition, $field, string $field2 = '', bool $date = false): array
    {
//        return array_filter($data, function ($user) use ($keyword, $condition, $field, $field2, $date) {
//
//            if($this->search_childs($user,$keyword,$condition,$field,$field2,$date)){
////                return true;
//                /**
//                 * 业绩总表匹配附属订单
//                 */
//                if (isset($user['childs'])){
//                    $chData = [];
//                    foreach ($user['childs'] as $child) {
//                        if ($this->search_childs($child,$keyword,$condition,$field,$field2,$date)){
//                            $chData[] = $child;
//                        }
//                    }
//                    $user['childs'] = $chData;
//                }
//                return true;
//            }
//            /**
//             * 主订单不符合 匹配附属订单
//             */
//            if (isset($user['childs'])){
//                $chData = [];
//                foreach ($user['childs'] as $child) {
//                    if ($this->search_childs($child,$keyword,$condition,$field,$field2,$date)){
//                        $chData[] = $child;
//                    }
//                }
//                $data[] = $chData;
//            }
//            return false;
//        });
        foreach ($data as $k => &$datum) {
            if ($this->search_childs ($datum, $keyword, $condition, $field, $field2, $date)) {
                /**
                 * 业绩总表匹配附属订单
                 */
                if (isset($datum['childs'])) {
                    $chData = [];
                    foreach ($datum['childs'] as $child) {
                        if ($this->search_childs ($child, $keyword, $condition, $field, $field2, $date)) {
                            $chData[] = $child;
                        }
                    }
                    $datum['childs'] = $chData;
                }
                continue;
            }


            /**
             * 主订单不符合 匹配附属订单
             */
            if (isset($datum['childs'])) {
                foreach ($datum['childs'] as $childs) {
                    if ($this->search_childs ($childs, $keyword, $condition, $field, $field2, $date)) {
                        $data[] = $childs;
                    }
                }
            }
            //删除主订单
            unset($data[$k]);
        }
        return $data;
    }

    public function search_childs($user, $keyword, $condition, $field, $field2, $date)
    {
        $orgModel = new UserOrganization();
        $systemSer = new System();
        $keyword_arr = ['get_uid', 'out_uid', 'sale_uid', 'transaction_uid', 'cw_status', 'c_status','order_status','business_id'];
        $certificate_arr = ['certificate_leixing', 'certificate_leibie', 'certificate_dengji', 'certificate_zhuanye'];

        foreach ($user as $k => $value) {
            /**
             * 业绩总表筛选附属业务
             */
            if ($condition === $this->eq) {
                if ($keyword === $k) {
                    if (in_array($keyword, $certificate_arr)) {
                        $cert_key = array_search($keyword, $certificate_arr);
                        if(is_array ($user['certificate_id'])){
                            if (array_key_exists($cert_key, $user['certificate_id'])) {
                                if ($field == $user['certificate_id'][$cert_key]) {
                                    return true;
                                }
                            }
                        }
                    }
                    //如果是人员检索取text
                    if (in_array($keyword, $keyword_arr)) {
                        if (is_array($value)) $value = $value['text'];
                    } else {
                        if (is_array($value)) $value = $value['id'];
                    }

                    if ($field == $value) return true;
                }
            }
            if ($condition === $this->like) {
                if ($keyword === $k) {
                    if (is_array($value)) {
                        $value = $value['text'];
                    }

                    if (stripos($value, $field) !== false) {
                        return true;
                    }

                }
            }
            if ($condition === $this->between) {

                if ($keyword === $k) {

                    if ($date) {
                        $field = strtotime($field);
                        $field2 = strtotime($field2);
                        $value = strtotime($value);
                    }else{
                        $value = (int)$value;
                    }

                    if ($field <= $value && $value <= $field2) {
                        return true;
                    }
                }
            }
            //部门 范围查询
            if ($condition === $this->in) {
                if ($keyword === $k) {
                    $orgArr = explode(',', rtrim($systemSer->getOrgIdStr($field, $orgModel->getStructure()), ','));
                    //如果是部门检索取ID
                    if (is_array($value)) {
                        $value = $value['id'];
                    }
                    if (in_array($value, $orgArr)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 业绩总表查询人才附属业务详情
     */

    public function getOtherBusiness($id)
    {
        $otherCerBusinessModel = new OrderOtherCertificateBusiness();
        $transactionModel = new OrderTransactionRecord();
        $messagesModel = new UserMessage();

        $chids = $otherCerBusinessModel->getWhereIdDataModel($id);
        if (!empty($chids)) {
            if ($chids['status']['id'] == 11){
                return [];
            }
            //处理拼接返回表格参数
            //出证价
//            $chids['contract_price'] = $chids['cost_price'] ?: 0;
            //出证价
            $chids['contract_price'] = $chids['sale_price'];
            $chids['sale_price'] = $chids['cost_price'];

            //最新收款时间
            $chids['get_price_date'] = $transactionModel->getFindDataModel($chids['certificate_code'], $chids['id'], 1) ?: "";
            //已收款合计
            $skall = $transactionModel->getCerTypeAllPriceModel($chids['certificate_code'], $chids['id'], 1);
            $chids['get_price_all'] = $skall['all_price'] ?: '';
            //最新付款时间
            $chids['out_price_date'] = $transactionModel->getFindDataModel($chids['certificate_code'], $chids['id'], 2) ?: '';
            //已付款合计
            $fkall = $transactionModel->getCerTypeAllPriceModel($chids['certificate_code'], $chids['id'], 2);
            $chids['out_price_all'] = $fkall['all_price'] ?: '';
            $chids['leixing'] = "证书办理";
            $chids['out_uid'] = $chids['get_uid'];
            $chids['out_org'] = $chids['get_org'];
            //最新一条催款记录
            $ckData = $messagesModel->getPressFindEmpty($chids['id'], $chids['certificate_code']);
            if (!empty($ckData)) {
                $chids['ckjl'] = 1; //有
            } else {
                $chids['ckjl'] = 2;//没有
            }
            return $chids;
        }
        return [];
    }

    /**
     * @param $uid //登录人
     * @return array[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSpPersonList($uid)
    {
        $data = [
            'finance_list' => [],
            'teller_list' => []
        ];
        $nameArr = config('sp_person.nameArr');
        //获取uid数据
        $user = new \app\model\User();
        $udata = $user->getIdUserData($uid);
        if (!$uid || !$udata) return $data;
        $department_id = is_array($udata['department_id']) ? $udata['department_id']['id'] : $udata['department_id'];
        if (!$department_id) return $data;
        //如果是admin登录 获取所有部门  否则获取员工所在分公司
        if($udata['username'] == 'admin'){
            $userOrg = new UserOrganization();
            $department_list = $userOrg->getStructureArr();
            $department_ids = array_column($department_list,'id');
        }else{
            //获取员工所在分公司
            $system = new System();
            $company_id = $system->getOrgFgsId($department_id);
            //获取公司下所有部门
            $department_ids = SystemUser::getDepartmentAllUser($company_id);
        }

        //包含财务和出纳的角色ID
        $userPosition = new UserPosition();
        $finance_roles_ids = $userPosition->getRoleIds($nameArr['finance_name']);
        //查询该分公司下所有财务
        $data['finance_list'] = $user->getWhereInData($department_ids, $finance_roles_ids);
        $teller_roles_ids = $userPosition->getRoleIds($nameArr['teller_name']);
        //查询该分公司下所有出纳
        $data['teller_list'] = $user->getWhereInData($department_ids, $teller_roles_ids);
        return $data;
    }

    /**
     * 业绩总表获取查询redisKey集合
     * @param array $uidArr //用户数组
     */
    public function getOrderDataIdData(array $uidArr)
    {
        $orderEnterpriseCertificateBusinessModel = new OrderEnterpriseCertificateBusiness();
        $otherCertificateBusinessModel = new OrderOtherCertificateBusiness();
        $OrderEnterpriseCompanyModel = new OrderEnterpriseCompany();
        $OrderEnterpriseExchangeModel = new OrderEnterpriseExchange();

        //查询证书办理订单
        $others = $otherCertificateBusinessModel->getRandDataModel($uidArr);
        //查询企业证书
        $businesss = $orderEnterpriseCertificateBusinessModel->getRandDataModel($uidArr);
        //查询公司证书
        $companyData = $OrderEnterpriseCompanyModel->getUserRandDataModel($uidArr);
        //查询倒证
        $exchangeData = $OrderEnterpriseExchangeModel->getUserRandDataModel($uidArr);

        return array_merge ($others,$businesss,$companyData,$exchangeData);
    }

}